package com.juyu.miliao.api.dao.redis;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.juyu.miliao.api.dao.mysql.UserSignInInfoDao;
import com.juyu.miliao.api.domain.UserSignInInfo;
import com.juyu.miliao.api.domain.UserSignInLog;
import com.juyu.miliao.api.util.RedisKeys;
import com.juyu.miliao.common.exception.IllegalParamException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author xuanqicheng
 * @create 2019-12-09:47
 */
@Repository
public class UserSignInInfoDaoRedis {

    @Autowired
    private UserSignInInfoDao userSignInInfoDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 获取用户登录日志信息
     * @param userId
     * @return
     */
    public UserSignInLog getUserSignInLog(long userId) {

        String key = RedisKeys.USER_SIGN_IN_LOG_KEY.getValue(userId);

        Map<Object, Object> map = stringRedisTemplate.opsForHash().entries(key);

        if(map!=null){
            UserSignInLog userSignInLog = new UserSignInLog();

            BeanUtils.copyProperties(map,userSignInLog);

            return userSignInLog;
        }else{
            return null;
        }
    }


    public UserSignInInfo getUserSignInfoByDay(int day) throws IOException {
        String key = RedisKeys.USER_SIGN_IN_INFO_BY_DAY_KEY.getValue(day);
        String temp = stringRedisTemplate.opsForValue().get(key);
        if(StringUtils.isNotBlank(temp)){
            UserSignInInfo userSignInInfo=objectMapper.readValue(temp,UserSignInInfo.class);
            return userSignInInfo;
        }else{
            UserSignInInfo userSignInInfo=userSignInInfoDao.getUserSignInfoByDay(day);
            if(userSignInInfo!=null){
                stringRedisTemplate.opsForValue().set(key,objectMapper.writeValueAsString(userSignInInfo),
                        5, TimeUnit.MINUTES);
                return userSignInInfo;
            }else{
                throw new IllegalParamException("签到信息为空");
            }
        }
    }

    public List<UserSignInInfo> getUserSignInfoList() {
        String key = RedisKeys.USER_SIGN_IN_LIST_KEY.getValue();
        String temp = stringRedisTemplate.opsForValue().get(key);
        if(StringUtils.isNotBlank(temp)){
            List<UserSignInInfo> list = JSONArray.parseArray(temp, UserSignInInfo.class);
            return list;
        }else{
            List<UserSignInInfo> list = userSignInInfoDao.selectList();
            if(list.size()>0){
                stringRedisTemplate.opsForValue().set(key, JSONObject.toJSONString(list),10,TimeUnit.MINUTES);
            }
            return list;
        }
    }

    public boolean getSignInStatus() {
        String temp =stringRedisTemplate.opsForValue().get("user:sign-in:switch");
        if(StringUtils.isNotBlank(temp)){
            return Boolean.parseBoolean(temp);
        }else{
            return true;
        }
    }
}
